<?php

namespace Database\Seeders;

use App\Models\TextbookAudio\TextbookAudio;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class TextbookAudioDataSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $textbook_audios = DB::connection('rjmysql')->table('textbook_audios')->get()->toArray();
        $textbook_audios = json_decode(json_encode($textbook_audios), true);

        foreach ($textbook_audios as $textbook_audio) {
            if (DB::table('categorys')->where('id', $textbook_audio['category_id'])->exists()) {
                TextbookAudio::query()->updateOrCreate(
                    ['id' => $textbook_audio['id']],
                    $textbook_audio
                );
            }
        }

        $this->command->info('歌曲歌谣数据填充完成！');
    }
}
